import QtQuick 2.0
import QtQuick.Controls 2.12

Rectangle {
    id: textEdit
    implicitWidth: 60
    implicitHeight: 40
    border.color: "#f0f0f0"
    border.width: 2
    property string text: ""
    Flickable {
         id: flick

         anchors.fill: parent
         contentWidth: edit.paintedWidth
         contentHeight: edit.paintedHeight
         clip: true

         function ensureVisible(r)
         {
             if (contentX >= r.x)
                 contentX = r.x;
             else if (contentX+width <= r.x+r.width)
                 contentX = r.x+r.width-width;
             if (contentY >= r.y)
                 contentY = r.y;
             else if (contentY+height <= r.y+r.height)
                 contentY = r.y+r.height-height;
         }
         ScrollBar.vertical: ScrollBar {
             policy: ScrollBar.AsNeeded
         }
         ScrollBar.horizontal: ScrollBar {
             policy: ScrollBar.AsNeeded
         }

         TextEdit {
             id: edit
             width: flick.width
             focus: true
//             wrapMode: TextEdit.Wrap
             text: textEdit.text
             selectByMouse: true
             onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
         }
     }

    function readText() {
        return edit.text
    }
}
